package com.tear.controller;

import com.tear.bean.User;
import com.tear.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/list")
    public String  list(Model model){
        List<User> users=userService.findAll();
        model.addAttribute("users",users);
        return  "index";
    }
    @RequestMapping("/")
    public String  index(){
        return  "redirect:/list";
    }
    //上面是前往主页

    @RequestMapping("/insertPage")
    public String  Toadd(){
        return  "add";
    }
    @RequestMapping("/insert")
    public String  add(User user){
        userService.add(user);
        return "redirect:/list";
    }

    @RequestMapping("/updatepage")
    public String  GoPage(Model model,int id){
        User user=userService.findById(id);
        model.addAttribute("us",user);
        return  "edit";
    }

    @RequestMapping("/edit")
    public String  update(User user){
        userService.update(user);
        return  "redirect:/list";
    }


    @RequestMapping("/delete")
    public String  delete(int id){
        userService.delete(id);
        return "redirect:/list";
    }
}
